package com.sugon.modules.report.service.impl;

import com.sugon.dao.DeviceInfoDao;
import com.sugon.dao.MesShiftprojectDao;
import com.sugon.entity.DeviceInfoEntity;
import com.sugon.modules.analyse.model.vo.DeviceKpiStatParamVO;
import com.sugon.modules.kpi.consts.DeviceOrganizationLevel;
import com.sugon.modules.kpi.model.ShiftAccuStatVO;
import com.sugon.modules.report.service.IShiftProjectManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author ： YuXD
 * @description： 工厂排班次计划Manager实现类
 * @date ： 2020-11-16 下午 5:30
 * @version: 1.0
 */
@Service
public class ShiftProjectManagerImpl implements IShiftProjectManager {

    @Autowired
    private MesShiftprojectDao mesShiftprojectDao;
    @Autowired
    private DeviceInfoDao deviceInfoDao;

    /**
     * doYearlyLineShiftStat
     *
     * @param kpiStatParam
     * @return
     */
    @Override
    public Map<Integer, Double> doYearlyLineShiftStat(DeviceKpiStatParamVO kpiStatParam) {
        List<ShiftAccuStatVO> shiftAccuStat;
        cn.hutool.core.lang.Assert.checkBetween(kpiStatParam.getLevel(), DeviceOrganizationLevel.LINE, DeviceOrganizationLevel.DEVICE);
        if (kpiStatParam.getLevel().equals(DeviceOrganizationLevel.LINE)) {
            shiftAccuStat = mesShiftprojectDao.doYearlyLineShiftStat(kpiStatParam.getYearly(), kpiStatParam.getDeviceOrganizationCode());
        } else {
            DeviceInfoEntity deviceInfo = deviceInfoDao.getInfoByDevCode(kpiStatParam.getDeviceOrganizationCode());
            Assert.notNull(deviceInfo, "设备[" + kpiStatParam.getDeviceOrganizationCode() + "]信息不存在");
            shiftAccuStat = mesShiftprojectDao.doYearlyLineShiftStat(kpiStatParam.getYearly(), deviceInfo.getDevProductCode());
        }
        if (shiftAccuStat == null) {
            shiftAccuStat = new ArrayList<>();
        }
        return shiftAccuStat.stream().collect(Collectors.toMap(ShiftAccuStatVO::getMonth, ShiftAccuStatVO::getHourTotal));
    }

}
